/**
 * Created by LeungZ on 2016/10/17.
 */
import jsYaml from 'js-yaml';
class Create {
    constructor($state, service, appSrv) {
        "ngInject";
        this.fData = {};

        if ($state.params.k8sCode) {
            this.fData.cluster = {
                clusterCode: $state.params.k8sCode
            };
        }
        if ($state.params.lesseeCode) {
            this.fData.namespaceCode = $state.params.lesseeCode;
        }

        if ($state.params.appId) {
            this.fData.app = {
                id: $state.params.appId
            }
        }

        if ($state.params.serId && service) {
            try {
                var spec = jsYaml.safeLoad(service.yamlContent).spec;
                this.fData.services = [{
                    serviceId: $state.params.serId,
                    podAppName: service.serviceCode,
                    podNum: spec.replicas
                }];

                spec = spec.template;
                if (spec.metadata.labels.image) {
                    var image = spec.metadata.labels.image.split('-');
                    this.fData.services[0].appImg = {
                        id: image[0],
                        ver: {
                            id: +image[1]
                        }
                    };
                }

                spec = spec.spec;
                if (spec.nodeSelector) {
                    this.fData.services[0].groups = [];
                    angular.forEach(Object.keys(spec.nodeSelector), item=> {
                        this.fData.services[0].groups.push(item);
                    });
                }
                if (spec.containers[0].volumeMounts && spec.containers[0].volumeMounts.length) {
                    this.fData.services[0].volumes = [];
                    this.fData.services[0].logVols = [];
                    angular.forEach(spec.containers[0].volumeMounts, (vol, index)=> {
                        if (spec.volumes[index].hostPath.path === appSrv._logPath) {
                            this.fData.services[0].logVols.push({
                                name: vol.name,
                                path: vol.mountPath
                            });
                        } else {
                            this.fData.services[0].volumes.push({
                                name: vol.name,
                                mountPath: vol.mountPath,
                                hostPath: spec.volumes[index].hostPath.path
                            });
                        }
                    });
                }
                if (spec.containers && spec.containers.length) {
                    spec = spec.containers[0];
                    if (spec.resources && spec.resources.limits) {
                        this.fData.services[0].podCpu = +spec.resources.limits.cpu;
                        this.fData.services[0].podMem = +spec.resources.limits.memory.replace(appSrv._memUnit, '');
                    }
                    if(spec.env && spec.env.length){
                        this.fData.services[0].envs = [];
                        angular.forEach(spec.env, e=>{
                            this.fData.services[0].envs.push({
                                key: e.name,
                                value: e.value
                            })
                        })
                    }
                }
                throw '';
            }
            catch (e) {}
        }

        if ($state.current.name != "app-service-version-publish.deploy") {
            $state.go('app-service-version-publish.deploy');
        }
    }
}
export default Create